# -*- encoding=utf8 -*-
__author__ = "Bryan.chen"
__title__ = "大厅 - 兑换/购买金豆"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

class Shop_AddGoldBean:

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def converGoldBeanNum(self, cnNum):
        num = 0
        if cnNum.find("百万") > 0:
            cnNum = cnNum.replace("百万", "")
            num = float(cnNum) * 1000000
        elif cnNum.find("万") > 0:
            cnNum = cnNum.replace("万", "")
            num = float(cnNum) * 10000
        else:
            num = float(cnNum)
            
        return num
    
    def runTest(self, isUseRMB):
        
        poco("HjLobbyScene").wait_for_appearance()
        
        btnAddGold = poco("node_glod").child("gold_add")
        myGold = self.converGoldBeanNum(poco("node_glod").child("txt_gold_num").get_text()) # 金豆数量 
        myDiamond = self.converGoldBeanNum(poco("node_blueDiamond").child("txt_luckcoin_num").get_text()) # 钻石数量 
        
        btnAddGold.click([0.5, 0.5])

        poco("LbPurchaseLayer").wait_for_appearance()
        
        lbPurchaseLayer = poco("LbPurchaseLayer")
        
        if not lbPurchaseLayer.exists():
            # 超时没有弹出充值框
            return False

        btnSelectGoldBean = lbPurchaseLayer.offspring("select_btn1")
        btnSelectGoldBean.click([0.5, 0.5])
        
        boughtItems = set()
        boughtNew = True
        
        items1 = lbPurchaseLayer.offspring("item_list").offspring("item1")
        items0 = lbPurchaseLayer.offspring("item_list").offspring("item0")
        
        items = items1
        if isUseRMB:
            isExistRMBItem = False
            for item in items1:
                txtPrice = item.child("item_price").get_text()
                if txtPrice.find("￥")>=0:
                    isExistRMBItem = True
                    break
            if isExistRMBItem:
                items = items1
            else: 
                items = items0
                    
        
        
        while boughtNew:
            boughtNew = False
            
            for item in items:
                itemName = item.child("item_name").get_text()
                txtPrice = item.child("item_price").get_text()
                price = -1
                isRmbPrice = False
                if txtPrice.isnumeric():
                    price = float(item.child("item_price").get_text());
                else:
                    txt = item.child("item_price").get_text()
                    if txtPrice.find("￥") >= 0:
                        txtPrice = txtPrice.replace("￥", "")
                    price = float(txtPrice)
                    isRmbPrice = True
                
                if isUseRMB and not isRmbPrice:
                    continue
                
                if not isUseRMB and isRmbPrice:
                    print(u"人民币支付，跳过")
                    continue
                
                # 如果没有买过这个商品，则购买
                if itemName not in boughtItems:
                        
                    item.child("item_price_bg").click([0.5, 0.5])
                    
                    poco("Panel_AskBuy").wait_for_appearance()
                    
                    askBuyPanel = poco("Panel_AskBuy")

                    if not askBuyPanel.exists():
                        print(u"超时不弹出购买框")
                        boughtNew = True
                        break
                    
                    btnAskBuySure = askBuyPanel.offspring("btn_AskBuy_Sure")
                    btnAskBuySure.click([0.5, 0.5])
                    
                    if isUseRMB:
                        
                        poco("PayChoseTipLayer").wait_for_appearance()

                        payChoseTipLayer = poco("PayChoseTipLayer")
                        if not payChoseTipLayer.exists():
                            print("超时不弹出选择支付方式框")
                            break

                        btnWechat = payChoseTipLayer.offspring("Button_1")
                        btnWechat.click([0.5, 0.5])

                        sleep(3.0)
                        
                        snapshot("点击微信支付后弹出的界面")
                        
                        keyevent("BACK")
                    else:
                        snapshot()

                        txtBoughtResult = poco("ToastView").offspring("text_content").get_text()
                        if myDiamond < price:
                            assert_equal(txtBoughtResult, "钻石不足,无法购买", "钻石不足，无法购买")
                        else:
                            myNewDiamond = self.converGoldBeanNum(poco("node_blueDiamond").child("txt_luckcoin_num").get_text())
                            print(u"购买后的钻石数量:%s" % myNewDiamond)
                            assert_equal(myDiamond - price, myNewDiamond, "购买金豆，扣钻正常")
                            assert_equal(txtBoughtResult, "购买成功", "购买商品操作成功")
                    
                    boughtItems.add(itemName)
                    
                boughtNew = True
                break # 买一件商品就可以了
                    
            if boughtNew:
                break
        
        poco("LbPurchaseLayer").wait_for_appearance()
        
        btnClose = lbPurchaseLayer.offspring("return_btn")
        btnClose.click([0.5, 0.5])
            
    def main(self, args):
        self.runTest(False)
        sleep(2.0)
        self.runTest(True)

        return True

_instance = Shop_AddGoldBean()
runTest = _instance.main

